<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滑动动画效果</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }

        .container {
            text-align: center;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            max-width: 500px;
            width: 100%;
        }

        .demo-area {
            width: 100%;
            height: 200px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
            background-color: #f9f9f9;
            border-radius: 8px;
        }

        .demo-box {
            width: 150px;
            height: 150px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #2196F3;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
            border-radius: 8px;
        }

        .controls {
            margin-top: 20px;
        }

        button {
            padding: 10px 20px;
            margin: 0 10px;
            border: none;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #3e8e41;
        }

        .slide-in-left {
            animation: slideInLeft 0.5s ease-out forwards;
            transform: translate(-200%, -50%);
        }

        .slide-in-right {
            animation: slideInRight 0.5s ease-out forwards;
            transform: translate(100%, -50%);
        }

        .slide-out-left {
            animation: slideOutLeft 0.5s ease-in forwards;
        }

        .slide-out-right {
            animation: slideOutRight 0.5s ease-in forwards;
        }

        @keyframes slideInLeft {
            from { transform: translate(-200%, -50%); }
            to { transform: translate(-50%, -50%); }
        }

        @keyframes slideInRight {
            from { transform: translate(100%, -50%); }
            to { transform: translate(-50%, -50%); }
        }

        @keyframes slideOutLeft {
            from { transform: translate(-50%, -50%); }
            to { transform: translate(-200%, -50%); }
        }

        @keyframes slideOutRight {
            from { transform: translate(-50%, -50%); }
            to { transform: translate(100%, -50%); }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>滑动动画效果</h1>
        <div class="demo-area">
            <div class="demo-box" id="demoBox">滑动效果</div>
        </div>
        <div class="controls">
            <button onclick="slideInLeft()">左滑入</button>
            <button onclick="slideInRight()">右滑入</button>
            <button onclick="slideOutLeft()">左滑出</button>
            <button onclick="slideOutRight()">右滑出</button>
            <button onclick="resetAnimation()">重置</button>
        </div>
        <div class="code-section">
            <h3>CSS代码:</h3>
            <pre><code>.slide-in-left {
  animation: slideInLeft 0.5s ease-out forwards;
  transform: translateX(-100%);
}

.slide-in-right {
  animation: slideInRight 0.5s ease-out forwards;
  transform: translateX(100%);
}

.slide-out-left {
  animation: slideOutLeft 0.5s ease-in forwards;
}

.slide-out-right {
  animation: slideOutRight 0.5s ease-in forwards;
}

@keyframes slideInLeft {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}

@keyframes slideInRight {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

@keyframes slideOutLeft {
  from { transform: translateX(0); }
  to { transform: translateX(-100%); }
}

@keyframes slideOutRight {
  from { transform: translateX(0); }
  to { transform: translateX(100%); }
}</code></pre>
        </div>
    </div>

    <script>
        const box = document.getElementById('demoBox');

        function slideInLeft() {
            resetAnimation();
            box.classList.add('slide-in-left');
        }

        function slideInRight() {
            resetAnimation();
            box.classList.add('slide-in-right');
        }

        function slideOutLeft() {
            resetAnimation();
            box.classList.add('slide-out-left');
        }

        function slideOutRight() {
            resetAnimation();
            box.classList.add('slide-out-right');
        }

        function resetAnimation() {
            box.classList.remove('slide-in-left', 'slide-in-right', 'slide-out-left', 'slide-out-right');
            // 触发重绘
            void box.offsetWidth;
        }
    </script>
</body>
</html>